﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using SuperInterMarket.Data;

namespace SuperInterMarket.Mail
{
    public partial class ServerManage : Form
    {
        public ServerManage()
        {
            InitializeComponent();
        }
        private List<ServerConfig> GetServerConfigs()
        {
            List<ServerConfig> sConfigs = new List<ServerConfig>();
            XDocument doc = XDocument.Load(SysConstants.MailServerFile);
            XElement config = doc.Element("config");
            List<XElement> servers = config.Elements("server").ToList<XElement>();
            if (servers.Count > 0)
            {
                foreach (var item in servers)
                {
                    ServerConfig sConfig = new ServerConfig();
                    sConfig.MailType = item.Attribute("type").Value;
                    sConfig.MailServer = item.Element("name").Value;
                    sConfig.MailPort = item.Element("port").Value;
                    sConfigs.Add(sConfig);
                }
            }
            return sConfigs;
        }

        private void ServerManage_Load(object sender, EventArgs e)
        {

            ServerListDataGridView.DataSource = GetServerConfigs();
            
        }

        private void OkBt_Click(object sender, EventArgs e)
        {
            string mailType = MailTypeTxt.Text;
            string mailServer = MailServerTxt.Text;
            string mailPort = MailPortTxt.Text;
            bool flag = true;
            try
            {
                XDocument doc = XDocument.Load(SysConstants.MailServerFile);
                XElement config = doc.Element("config");
                var temp = (from server in config.Elements("server") where server.Attribute("type").Value == mailType select (XElement)server).SingleOrDefault();

                //List<XElement> servers = config.Elements("server").ToList<XElement>();
                if (temp != null)
                {

                    temp.SetElementValue("name", mailServer);
                    temp.SetElementValue("port", mailPort);
                    doc.Save(SysConstants.MailServerFile);
                }
                else
                {
                    XElement newE = new XElement("server");
                    newE.Add(new XAttribute("type", mailType));
                    newE.Add(new XElement("name", mailServer));
                    newE.Add(new XElement("port", mailPort));
                    config.AddFirst(newE);
                    doc.Save(SysConstants.MailServerFile);
                }
                
            }
            catch (Exception)
            {
                flag = false;
            }

            MessageBox.Show(flag==true?"操作成功":"操作失败");
            ServerListDataGridView.DataSource = GetServerConfigs();
        }
    }
}
